<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        .box{
            width: 500px;
            height: 300px;
            border:3px solid purple;
            position: fixed;
            top:0; bottom:0;
            left:0; right:0;
            margin:auto;
        }
        p{
            width:500px;
            height: 300px;
            text-align: center;
            line-height: 300px;
            font-size: 50px;
            position:absolute;
            z-index: -1;
            display: none;
        }
        p.active{
            display: block;
        }
        p:nth-child(1){
            background: rgb(195, 74, 13);
        }
        p:nth-child(2){
            background: orange;
        }
        p:nth-child(3){
            background: yellow;
        }
        p:nth-child(4){
            background: green;
        }
        p:nth-child(5){
            background: yellowgreen;
        }
        .click{
            width:300px;
            margin:250px auto auto;
            display: flex;
            justify-content: space-between;
        }
        aside{
            width: 20px;
            height: 20px;
            border-radius:50%;
            border:3px solid aquamarine;
        }
        aside.active{
            background: #000;
        }
        .change{
            width:20px;
            height:50px;
            background: red;
        }
        .left{
            position: absolute;
            top:0; left:0;
            bottom:0; right:0;
            margin:auto 0;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .right{
            position: absolute;
            top:0; left:0;
            bottom:0; right:0;
            margin:auto 0 auto auto;
            display: flex;
            justify-content: center;
            align-items: center;
        }
    </style>
</head>
<body>
    <div class="box">
        <p class="active">one</p>
        <p>two</p>
        <p>three</p>
        <p>four</p>
        <p>five</p>
        <div class="click">
            <aside class="active"></aside>
            <aside></aside>
            <aside></aside>
            <aside></aside>
            <aside></aside>
        </div>
    <div class="change left"> &lt;</div>
    <div class="change right"> &gt;</div>
    </div>
</body>
</html>
<script>
    class banner {
        constructor(box,setTime=2000){
            this.box = box;
            this.setTime = setTime;
            this.content = box.querySelectorAll(".box>p");
            this.click = box.querySelectorAll(".click > aside");
            this.left = box.querySelector(".left");
            this.right = box.querySelector(".right");
            //设置默认索引
            this.i = 0;

            this.public();

            this._click();
            
            this.left_click();

            this.right_click();

            this.timerId = null;
            this.autoplay();
            
            this.mouseEvent();
        }
        //实现.content和.click中class的切换
        public(){
            this.click.forEach(c=>c.className="");
            this.click[this.i].className="active";
            this.content.forEach(content=>content.className="");
            this.content[this.i].className="active";
        }
        //点击.click进行内容切换
        _click(){
            this.click.forEach((c,index) => {
                c.addEventListener("click",e=>{
                this.i = index;
                this.public();
                })
            })
        }

        left_click(){
            this.left.addEventListener("click",e=>{
                this.i-=1;
                if(this.i<0){
                    this.i=this.content.length-1;
                }
                this.public();
            })
        }  
  
        right_click(){
            this.right.addEventListener("click",e=>{
                this.i+=1;
                if(this.i>this.content.length-1){
                    this.i=0;
                }
                this.public();
            })
        }

        autoplay(){
            this.timerId = setInterval(()=>{
                this.i+=1;
                if(this.i>this.content.length-1){
                    this.i=0;
                }
                this.public();
            }, this.setTime);  
        }

        mouseEvent(){
            this.box.addEventListener("mouseenter",()=>{
                clearInterval(this.timerId);
                console.log("停止");
            })
            this.box.addEventListener("mouseleave",()=>{
                this.autoplay();
                console.log("开始");
            })
        }
    }
    const box = document.querySelector(".box");
    const res = new banner(box);
</script>